<?php 

require ('../include/web.init.inc.php');
extract ( $_POST);
header("Content-type: application/json");
$current_user_info = UserSession::getSessionInfo();
$out_trade_no = time();
if ($type == "私人律师") {
	$input_data = array ('user_id'=>$current_user_info["user"]["id"],'privite_id'=>$real_id,'lawer_id'=>$lawer_id);
	$real_id = Law::addPriviteLaw($input_data);

	$out_trade_no = time();
	

	if ($pay_type == '余额') {
		$user = TbUser::getUserById($current_user_info["user"]["id"]);
		if ($user["total_fee"] >= $price) {
			$input_data = array ('user_id'=>$current_user_info["user"]["id"],'real_id'=>$real_id,'user_name'=>$current_user_info["user"]["nick_name"],'lawer_id'=>$lawer_id,'out_trade_no'=>$out_trade_no,'price' => $price,'type'=>"私人律师",
				'pay_type'=>$pay_type,'status'=>0);
			$module_id = Order::addOrder ( $input_data );
			$fee = $user["total_fee"] - $price;
			TbUser::updateUser(array('total_fee' => $fee),$current_user_info["user"]["id"]);
			Order::payOrderSuccess($out_trade_no,$out_trade_no,'余额');
		} else {
			$ret = json_encode(array("code"=>0,"msg"=>"余额不足"));
			echo $ret;die();
		}
	} else {
		$input_data = array ('user_id'=>$current_user_info["user"]["id"],'real_id'=>$real_id,'user_name'=>$current_user_info["user"]["nick_name"],'lawer_id'=>$lawer_id,'out_trade_no'=>$out_trade_no,'price' => $price,'type'=>"私人律师");
		$module_id = Order::addOrder ( $input_data );
	}
} else {
	
	$lawer_id = 0;
	$accept = 1;
	if ($type == "案件竞标预付") {
		$lawer_id = Compete::getLawerIdById($real_id);
	} else if ($type == "案件竞标尾款") {
		$lawer_id = Compete::getLawerIdById($real_id);
	} else if ($type == "案件委托") {
		$lawer_id = Proxy::getLawerIdById($real_id);
	} else if ($type == "1vs1咨询") {
		$lawer_id = $real_id;
	} else if ($type == "预约面谈") {
		$lawer_id = $real_id;
		$accept = 0;
	} else if ($type == "电话咨询") {
		$lawer_id = $real_id;
	}

	if ($pay_type == '余额') {
		$user = TbUser::getUserById($current_user_info["user"]["id"]);
		if ($user["total_fee"] >= $price) {
			$input_data = array ('user_id'=>$current_user_info["user"]["id"],'real_id'=>$real_id,'user_name'=>$current_user_info["user"]["nick_name"],'lawer_id'=>$lawer_id,'out_trade_no'=>$out_trade_no,'price' => $price,
			'type'=>$type,'address'=>$address,'pay_type'=>$pay_type,'case_type'=>$case_type,'phone'=>$phone,'face_time'=>$face_time,'status'=>0,'accept'=>$accept);
			$module_id = Order::addOrder ( $input_data );
			$fee = $user["total_fee"] - $price;
			Order::payOrderSuccess($out_trade_no,$out_trade_no,'余额');
			TbUser::updateUser(array('total_fee' => $fee),$current_user_info["user"]["id"]);
		} else {
			$ret = json_encode(array("code"=>0,"msg"=>"余额不足"));
		}
	} else {
		$input_data = array ('user_id'=>$current_user_info["user"]["id"],'real_id'=>$real_id,'user_name'=>$current_user_info["user"]["nick_name"],'lawer_id'=>$lawer_id,'out_trade_no'=>$out_trade_no,'price' => $price,
		'type'=>$type,'address'=>$address,'pay_type'=>$pay_type,'case_type'=>$case_type,'phone'=>$phone,'face_time'=>$face_time,'accept'=>$accept);
		$module_id = Order::addOrder ( $input_data );	
	}

	
}

// if($user_info){
$ret = json_encode(array("code"=>1,"data"=>array("id"=>$module_id,"out_trade_no"=>$out_trade_no)));
// }else{
// 	$ret = json_encode(array("code"=>0,"msg"=>"用户名密码错误"));
// }

echo $ret;